package com.xinqi.modules.user.users.user.service.impl;

import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.xinqi.common.base.error.CommonErrors;
import com.xinqi.common.base.response.PageInfo;
import com.xinqi.common.base.response.Param;
import com.xinqi.common.mybatis.utils.Pages;
import com.xinqi.common.mybatis.utils.Queries;
import com.xinqi.modules.user.user.domain.UserExtendEntity;
import com.xinqi.modules.user.user.dto.req.UserExtendCreateDTO;
import com.xinqi.modules.user.user.dto.req.UserExtendDeleteDTO;
import com.xinqi.modules.user.user.dto.req.UserExtendQueryDTO;
import com.xinqi.modules.user.user.dto.req.UserExtendUpdateDTO;
import com.xinqi.modules.user.user.dto.rsp.UserExtendResultDTO;
import com.xinqi.modules.user.users.user.convert.UserExtendConvert;
import com.xinqi.modules.user.users.user.manager.UserExtendManager;
import com.xinqi.modules.user.users.user.service.UserExtendService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.validation.annotation.Validated;

import java.util.List;
import java.util.Objects;
import java.util.Optional;
import java.util.stream.Collectors;


/**
 * 用户扩展信息服务层实现
 *
 * @author: wenjie
 * @date: 2023/05/17
 */
@Validated
@Service("userExtendService")
@RequiredArgsConstructor
public class UserExtendServiceImpl implements UserExtendService {

    private final UserExtendConvert userExtendConvert;
    private final UserExtendManager userExtendManager;

    /**
     * 分页查询 用户扩展信息数据
     */
    @Override
    @DS("slaver")
    public PageInfo<UserExtendResultDTO> page(Param pageable, UserExtendQueryDTO query) {
        IPage<UserExtendEntity> page = userExtendManager.page(Pages.page(pageable), wrapper(query));
        return Pages.convert(page, userExtendConvert::convert);
    }

    /**
     * 查询 用户扩展信息数据
     */
    @Override
    public List<UserExtendResultDTO> find(UserExtendQueryDTO query) {
        return userExtendManager.list(wrapper(query)).stream()
            .map(userExtendConvert::convert).collect(Collectors.toList());
    }

    private LambdaQueryWrapper<UserExtendEntity> wrapper(UserExtendQueryDTO query) {
        LambdaQueryWrapper<UserExtendEntity> wrapper = Queries.lambda();
        Queries.accept(query.getId(), v -> wrapper.eq(UserExtendEntity::getId, v));
        return wrapper;
    }

    /**
     * 根据id查询 用户扩展信息数据
     */
    @Override
    public UserExtendResultDTO findById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return Optional.ofNullable(userExtendManager.findById(id)).map(userExtendConvert::convert).orElse(null);
    }

    /**
     * 新增 用户扩展信息数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Long insert(UserExtendCreateDTO dto) {
        UserExtendEntity entity = userExtendConvert.create(dto);
        userExtendManager.save(entity);
        return entity.getId();
    }

    /**
     * 根据id修改 用户扩展信息数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean updateById(UserExtendUpdateDTO dto) {
        Optional.ofNullable(userExtendManager.getById(dto.getId()))
            .orElseThrow(() -> CommonErrors.BAD_REQUEST.asException("数据不存在"));
        UserExtendEntity entity = userExtendConvert.update(dto);
        return userExtendManager.updateById(entity);
    }

    /**
     * 根据id删除 用户扩展信息数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(Long id) {
        CommonErrors.BAD_REQUEST.check(Objects.nonNull(id), "ID不能为空");
        return userExtendManager.removeById(id);
    }

    /**
     * 根据id删除 用户扩展信息数据
     */
    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean deleteById(UserExtendDeleteDTO dto) {
        return deleteById(dto.getId());
    }


}
